package com.sinosoft.ptas.control;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.sinosoft.framework.core.api.domain.PmsData;
import com.sinosoft.framework.core.common.utils.ajax.ResponseData;
import com.sinosoft.ptas.entity.ReportEntity;
import com.sinosoft.ptas.service.ReportService;
import com.sinosoft.ptas.system.commom.PtasConstant;
import com.sinosoft.ptas.system.security.SecurityUtil;
import com.sinosoft.ptas.system.util.FileUploadingUtil;

@Controller  
@RequestMapping(value = "/files")  
public class FileController {  
	
	@Autowired
	ReportService reportService;
      
	  /*  @RequestMapping(value = "/", method = RequestMethod.GET)  
	    public String listt(HttpServletRequest request, HttpServletResponse response, Model model) {  
	    	//保存文件的路径
	        String iniFileDir = iniFileDir(request);  
	          
	        System.out.println(request.getAttribute("files"));
	        Map<String, String> fileMap = FileUploadingUtil.getFileMap();
	        System.out.println(fileMap);
	        model.addAttribute("files", FileUploadingUtil.getFileMap());  
	        return "files/list";  
	    } */ 
	      
	    /**
	     * 上传文件
	     * @param request
	     * @param reportId
	     * @return
	     */
	    @RequestMapping(value = "/upload", method = RequestMethod.POST)
	    @ResponseBody
	    @Transactional(rollbackFor=Exception.class)
	    public ResponseData doUpload(HttpServletRequest request,String reportId) {
	    	@SuppressWarnings("unused")
			String filePath="";
	        iniFileDir(request);  
	        ReportEntity report=new ReportEntity();
	        try {  
	            MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;  
	            Map<String, String> uploadedFiles = FileUploadingUtil.upload(mRequest.getFileMap());  
	  
	            Iterator<Entry<String, String>> iter = uploadedFiles.entrySet().iterator();  
	            while (iter.hasNext()) {  
	                Entry<String, String> each = iter.next();  
	                System.out.print("Uploaded File Name = " + each.getKey());  
	                System.out.println(", Saved Path in Server = " + each.getValue()); 
	                filePath=each.getValue();
	            }
	            report.setId(reportId);
	            report.setFilePath(filePath);
	            reportService.updateReportForFilePath(report);
	            return new ResponseData(true,filePath);
	        } catch (Exception e) {  
	            e.printStackTrace();  
	            return new ResponseData(false,null,e.getMessage(),null);
	        }  
	    }  
	   /**
	    * 初始化文件的保存路径
	    */
	    private String iniFileDir(HttpServletRequest request) { 
	    	PmsData pmsData = (PmsData)SecurityUtil.getSessionValue(PtasConstant.pms_app_content);
	        FileUploadingUtil.FILEDIR = request.getSession().getServletContext().getRealPath("/") +"resources//book//"+ pmsData.getOname();
	        if (FileUploadingUtil.FILEDIR == null) {  
	            FileUploadingUtil.FILEDIR = request.getSession().getServletContext().getRealPath("/")+"resources//book//"+pmsData.getOname(); 
	        }  
	        return FileUploadingUtil.FILEDIR;
	    }
}  
